package solutions.ferpa.ztest.intf;

import br.com.telescope.adsrt.dom.DomOpcao;
import br.com.telescope.adsrt.lib.*;
import br.com.telescope.t2.ServiceModel;
import br.com.telescope.t2.exception.ApplicationRuntimeException;
import br.com.telescope.t2.feature.CustomFeature;
import br.com.telescope.t2.feature.MethodFeature;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import solutions.ferpa.ztest.ent.*;

/**
 * Importação de Logs.
 * <p>
 * Importa os arquivos em texto que foram exportados pela interface 
 * UTL_LOGS_EXPORT para os logs novamente.
 * <P>
 * Telescope origin: RTLOG.UTL_LOGS_IMPORT
 * <P>
 * @author TELESCOPE - Build task T2 Web Interface Generator
 * <P>
 * @version 0.1
 */
public class UtlLogsImport extends ServiceModel {
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    public static final String GLOBAL_NAME = "RTLOG.UTL_LOGS_IMPORT";
    
    @Override
    public String globalName() {
        return GLOBAL_NAME;
    }
    
    {
        setTitle("Importação de Logs");
    }
    
    
    // Record items
    private DomOpcao aArquivosLogs;
    
    // Query items
    
    @Override
    public void initAttributes() {
        
        aArquivosLogs = new DomOpcao();
        aArquivosLogs.context(ctx).name("ARQUIVOS_LOGS").label("Arquivos de logs").unbound(true).group(true);
        
    }
    
    @Override
    public String getDefaultOrderBy() {
        return "";
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="IMPORTA_LOGS">
    public class FImportaLogs extends CustomFeature {
        
        @Override
        public void initForm() {
            aArquivosLogs.setOptional(false);
            addField(aArquivosLogs, "rqd");
        }
        
        @Override
        public void preDisplay() {
            try {
                popularArquivosTexto();
            } catch (AdsException ex) {
                throw new ApplicationRuntimeException(ex);
            }
        }
        
        @Override
        public void posDisplay() {
            try {
                popularArquivosTexto();
            } catch (AdsException ex) {
                throw new ApplicationRuntimeException(ex);
            }
        }
        
        @Override
        public void onAction() {
            addSubmit(getFeature("IMPORTAR_LOGS")).label("Importar ");
        }
        
        {
            setFeatureName("IMPORTA_LOGS");
            setTitle("Importa arquivos texto para logs");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="IMPORTAR_LOGS">
    public class FImportarLogs extends MethodFeature {
        
        @Override
        public void run() {
            preRun();
            try {
                importarLogs();
            } catch (AdsException ex) {
                throw new ApplicationRuntimeException(ex);
            }
        }
        
        {
            setFeatureName("IMPORTAR_LOGS");
            setTitle("Importar ");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="initFeatures">
    
    @Override
    protected void initFeatures() {
        addFeature(new FImportaLogs());
        addFeature(new FImportarLogs());
        super.initFeatures();
    }
    
    @Override
    public String getDefaultFeature() {
        return "IMPORTA_LOGS";
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Getters and Setters">
    
    public String getArquivosLogs() {
        return aArquivosLogs.getValue();
    }
    
    public void setArquivosLogs(String value) {
        this.aArquivosLogs.setValue(value);
    }
    // </editor-fold>
    
    /**
     * Importar .
     * <p>
     * Importa logs.
     */
    private void importarLogs() throws AdsException {
        // <user-code id="806896" parentId="363959" hashcode="44c8571" length="764">
        // import java.io.File;
        
        beginTransaction();
        
        aArquivosLogs.getHtmlParameter(); 
        String pasta = ctx.getPreference("RTADM.LOGS_FOLDER");
        boolean active = ctx.isLogActive();
        ctx.setLogActive(false);
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(pasta + aArquivosLogs.getValue()));
            String line;
            while ( (line = br.readLine()) != null) {
                EntLogs log = new EntLogs(ctx);
                log.readFromTXT(line);
                log.ins();
            }
        } catch (Exception ex) {
            throw new AdsException(ex);
        } finally {
            ctx.setLogActive(active);
            try {
                if (br != null) br.close();
            } catch (IOException ex) {
                // Ignorar
            }
        }
        ctx.showSuccess("Log importado com sucesso!");
        
        endTransaction();
        // </user-code>
    }
    
    /**
     * Popular arquivos texto.
     */
    private void popularArquivosTexto() throws AdsException {
        // <user-code id="806897" parentId="364205" hashcode="-2168009d" length="588">
        String pasta = ctx.getPreference("RTADM.LOGS_FOLDER");
        if (pasta == null || pasta.length() == 0) {
            throw new AdsException("RTADM.LOGS_FOLDER_NOT_CONFIGURED",
                    AdsException.ERROR, "A pasta usada para salvar os logs exportados" +
                    " não foi configurada. Favor configurar a preferencia " +
                    " RTADM.LOGS_FOLDER");
        }
        File file = new File(pasta);
        File[] arqs = file.listFiles();
        for (int i = 0; i < arqs.length; i++ ) {
            if (arqs[i].getName().endsWith(".log")) {
                aArquivosLogs.addOption(arqs[i].getName(),arqs[i].getName());
            }
        }
        // </user-code>
    }
}
